package com.lemon.library.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
/**
 * Created by Wisn on 2018/5/22 下午2:55.
 */
public class GlideUtil {

    private static RequestOptions options;
    private static RequestOptions circleOptions;
    private static RequestOptions tempOptions;


    public static String getUrl(String url, int width) {
        if (url != null && url.contains("cdn.oudianyun")) {
            if (url.contains("@base@tag=imgScale")) {
                if (width > 0) {
                    url += "&w=" + width;
                }
                url += "&F=webp";
            } else {
                url += "@base@tag=imgScale";
                if (width > 0) {
                    url += "&w=" + width;
                }
                url += "&F=webp";
            }
        }
        return url;
    }

    public static void load(Context context, RequestOptions options, String url, int res, int width, ImageView imageView) {
        if (filterContext(context)) {
            if (!TextUtils.isEmpty(url) && url.contains("gif")) {
                Glide.with(context).load(url).apply(options).into(imageView);
            } else {
                Glide.with(context)
                        .load(res == 0 ? getUrl(url, width) : res)
                        .apply(options).into(imageView);
            }
        }
    }

    public static void load(Context context, String url, int width, ImageView imageView) {
        load(context, getRequestOptions(), url, 0, width, imageView);
    }

    public static void load(Context context, String url, ImageView imageView) {
        load(context, getRequestOptions(), url, 0, 0, imageView);
    }

    public static void load(Context context, int res, ImageView imageView) {
        load(context, getRequestOptions(), null, res, 0, imageView);
    }

    public static void load(Context context, int res, int defautRes, ImageView imageView) {
        load(context, getRequestOptions(defautRes), null, res, 0, imageView);
    }

    /**
     * 设置圆形图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadCircle(Context context, String url, ImageView imageView) {
        load(context, getCircleRequestOptions(), url, 0, 0, imageView);
    }


    public static void loadTarget(Context context, RequestOptions options, String url, int res, int width, SimpleTarget simpleTarget) {
        if (filterContext(context)) {
            Glide.with(context)
//                    .asBitmap()
                    .load(res == 0 ? getUrl(url, width) : res)
                    .apply(options).into(simpleTarget);
        }
    }

    public static void loadTarget(Context context, int res, int width, SimpleTarget simpleTarget) {
        loadTarget(context, getRequestOptions(), null, res, width, simpleTarget);
    }

    public static void loadTarget(Context context, int res, SimpleTarget simpleTarget) {
        loadTarget(context, getRequestOptions(), null, res, 0, simpleTarget);
    }

    public static void loadTarget(Context context, String url, SimpleTarget simpleTarget) {
        loadTarget(context, getRequestOptions(), url, 0, 0, simpleTarget);
    }

    public static boolean filterContext(Context context) {
        if (context == null) {
            return false;
        }

        if (context instanceof Activity) {
            if (((Activity) context).isFinishing()) {
                return false;
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && ((Activity) context).isDestroyed()) {
                return false;
            }
        }

        return true;

    }

    private static RequestOptions getRequestOptions() {
        if (options == null) {
            synchronized (GlideUtil.class) {
                if (options == null) {
                    options = new RequestOptions()
                            .disallowHardwareConfig()
//                            .placeholder(R.drawable.lib_replactimage)
//                            .error(R.drawable.lib_replactimage)
                            .format(DecodeFormat.PREFER_RGB_565)
//                            .fallback(R.drawable.lib_replactimage)
                            .diskCacheStrategy(DiskCacheStrategy.ALL);
                }
            }
        }
        return options;
    }

    private static RequestOptions getRequestOptions(int res) {
        if (tempOptions == null) {
            synchronized (GlideUtil.class) {
                if (tempOptions == null) {
                    return RequestOptions
                            .circleCropTransform()
                            .disallowHardwareConfig()
                            .placeholder(res)
                            .error(res)
                            .format(DecodeFormat.PREFER_RGB_565)
                            .fallback(res).diskCacheStrategy(DiskCacheStrategy.ALL);
                }
            }
        }
        return tempOptions;

    }

    private static RequestOptions getCircleRequestOptions() {
        if (circleOptions == null) {
            synchronized (GlideUtil.class) {
                if (circleOptions == null) {
                    return RequestOptions
                            .circleCropTransform()
                            .disallowHardwareConfig()
//                            .placeholder(R.drawable.lib_replactimage)
//                            .error(R.drawable.lib_replactimage)
                            .format(DecodeFormat.PREFER_RGB_565)
//                            .fallback(R.drawable.lib_replactimage)
                            .diskCacheStrategy(DiskCacheStrategy.ALL);
                }
            }
        }
        return circleOptions;
    }

}
